package cn.lizhangqu.kb.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.UnsupportedEncodingException;

import cn.lizhangqu.kb.R;
import cn.lizhangqu.kb.util.CommonUtil;
import cn.lizhangqu.kb.util.HttpUtil;
import cn.lizhangqu.kb.util.Toasts;

/**
 * Created by 柯尊诚 on 2015/11/5.
 * kzc
 */
public class EleActivity extends Activity{

    private EditText eField;
    private Button putBtn;
    private TextView resultTv;
    private TextView titleTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_e);
        init();
    }

    private void init() {
        titleTv = (TextView) findViewById(R.id.tv_e_title);
        eField = (EditText) findViewById(R.id.et_e_query);
        putBtn = (Button) findViewById(R.id.btn_e_put);
        resultTv = (TextView) findViewById(R.id.tv_e_result);


        titleTv.setText("请输入宿舍号：");
        putBtn.setOnClickListener(dataOnClick);
    }

    private View.OnClickListener dataOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String num = eField.getText().toString();
            if (num.isEmpty()) {
                Toasts.makeText("请输入宿舍号！");
                return;
            }
            HttpUtil.getEleQuery(EleActivity.this, num, new HttpUtil.QueryCallback() {
                @Override
                public String handleResult(byte[] result) {
                    try {

                        Document doc = Jsoup.parse(new String(result, "utf-8"));
                        Elements elements = doc.select("div.well-lg");
                        Element element = elements.get(0);
                        element.child(5).remove();
                        resultTv.setText(Html.fromHtml(element.toString()));
//                        resultTv.setText(element.toString());

                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }

                    return null;
                }
            });
//            getEleQuery(num);

        }
    };

    public void getEleQuery(String num) {
        AsyncHttpClient client = new AsyncHttpClient();
        final ProgressDialog dialog = CommonUtil.getProcessDialog(this, "正在获取电费信息。。。");
        dialog.show();
        RequestParams params = new RequestParams();
        params.add("num", num);
        client.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
        client.post(HttpUtil.ELE_URL, params, new TextHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Document doc = Jsoup.parse(responseString);
                Elements elements = doc.select("div.container");
                resultTv.setText(elements.text());
                Toasts.makeText("获取成功！！！");
                dialog.dismiss();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                dialog.dismiss();
                Toasts.makeText("获取失败！！！");
                resultTv.setText(responseString);
            }


        });
    }


}
